package com.weme.mine.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.QuickViewHolder
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.jeremyliao.liveeventbus.LiveEventBus
import com.osom.proto.message.NetworkOuterClass.LoginRequest
import com.weme.common.util.setOnFastClickListener
import com.weme.common.util.setVerticalLayoutManager
import com.weme.common.activity.WebViewActivity
import com.weme.common.arouter.ARouterHelper
import com.weme.common.arouter.ARouterPaths
import com.weme.common.base.BaseMvvmActivity
import com.weme.common.network.OutLoginBean
import com.weme.common.util.LiveConfig
import com.weme.common.util.UserMKHelper
import com.weme.common.util.UtilsApp
import com.weme.common.view.CustomDialogFragment
import com.weme.mine.R
import com.weme.mine.databinding.ActivitySettingsBinding
import com.weme.mine.viewmode.SettingsViewMode
import dev.kratos.api.user_server.v1.LogoutRequest

@Route(path = ARouterPaths.MINESETTINGSACTIVITY)
class SettingsActivity : BaseMvvmActivity<ActivitySettingsBinding, SettingsViewMode>() {
    override fun initView(savedInstanceState: Bundle?) {
        ImmersionBar.with(this).hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR).titleBar(mBinding.customTopNavigationBar).init()
        val settingItems = listOf(
            getString(com.example.resources.R.string.settings_language),
            getString(com.example.resources.R.string.settings_block),
            getString(com.example.resources.R.string.settings_privacy),
            getString(com.example.resources.R.string.settings_agreement),
            getString(com.example.resources.R.string.settings_about),
            getString(com.example.resources.R.string.settings_service),
        )
        mBinding.recyclerView.setVerticalLayoutManager()
        mBinding.recyclerView.adapter = settingsAdapter
        settingsAdapter.addAll(settingItems)
        settingsAdapter.setOnItemClickListener { _, _, position ->
            when (settingItems.get(position)) {
                getString(com.example.resources.R.string.settings_language) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_LANGUAGE_ACTIVITY)
                }
                getString(com.example.resources.R.string.settings_block) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_BLOCK_ACTIVITY)
                }
                getString(com.example.resources.R.string.settings_service) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_SERVICE_ACTIVITY)
                }
                getString(com.example.resources.R.string.settings_privacy) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_PRIVACY_ACTIVITY)
                }
                getString(com.example.resources.R.string.settings_agreement) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_AGREEMENT_ACTIVITY)
                }
                getString(com.example.resources.R.string.settings_about) -> {
                    ARouterHelper.navigateToActivity(this, ARouterPaths.MINE_ABOUT_ACTIVITY)
                }
            }
        }
        mBinding.srvLogout.setOnFastClickListener {
            val dialog = CustomDialogFragment.newInstance(title = getString(com.example.resources.R.string.settings_out), content = getString(com.example.resources.R.string.dialog_out_content), positiveButtonText = getString(com.example.resources.R.string.yes), negativeButtonText = getString(com.example.resources.R.string.cancel), listener = object : CustomDialogFragment.DialogClickListener {
                override fun onPositiveButtonClick() {


                    viewModel.outLogin(LogoutRequest.newBuilder().build())

                }

                override fun onNegativeButtonClick() {

                }
            })
            dialog.show(supportFragmentManager, "CustomDialog")
        }
     LiveEventBus.get<Any>(LiveConfig.NET_WORK_SETTINGS).observe(this) {
            if (it is OutLoginBean) {
                if (it.isOutService) {
                    UtilsApp.outLogin()
                    ARouterHelper.navigateToActivity(this@SettingsActivity, ARouterPaths.LOGIN_ACTIVITY)
                } else {
                    viewModel.outLoginApi(LoginRequest.newBuilder().setSession(UserMKHelper.decodeString(UserMKHelper.SESSION)).build())
                }

            }
        }
    }


    override fun onDestroy() {
        super.onDestroy()

    }


    private val settingsAdapter = object : BaseQuickAdapter<String, QuickViewHolder>() {
        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: String?) {
            val image = holder.getView<ImageView>(R.id.image)
            when(holder.absoluteAdapterPosition){
                0-> image.setImageResource(com.example.resources.R.drawable.svg_language)
                1-> image.setImageResource(com.example.resources.R.drawable.svg_block)
                2-> image.setImageResource(com.example.resources.R.drawable.svg_private_policy)
                3-> image.setImageResource(com.example.resources.R.drawable.svg_aggreement)
                4-> image.setImageResource(com.example.resources.R.drawable.svg_about)
                5-> image.setImageResource(com.example.resources.R.drawable.svg_contact)
            }
            holder.setText(R.id.tvName, item)


        }

        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            return QuickViewHolder(R.layout.setting_item_view, parent)
        }


    }

    override fun getViewModelClass(): Class<SettingsViewMode> = SettingsViewMode::class.java


}